import React from 'react'
import { connect } from 'dva'
import { Form ,Modal,message,Upload,Button} from 'antd'
import { Link, hashHistory } from 'dva/router'
import config from 'config/config'
import $ from 'jquery'
import FlagBtn from 'widget/FlagBtn/FlagView'
import { mapStateToProps } from 'utils/view_utils'
import styles from './userListStyle.less'
import ListPage from 'widget/PageTmpl/ListPage'
import classnames from 'classnames/bind';
let cx=classnames.bind(styles);
const confirm = Modal.confirm;



const userListView = function({dispatch, model, form}) {
  // 表单的子函数
  // const {resetFields, getFieldDecorator, validateFields, setFieldsValue} = form
  // 取出model对象，命名空间
  const { modelObj, namespace } = model
  // modelObj.xxx获取model中状态机的值
  let {products,areaList,submitFeikongLoading,backLoading,submitLoading,biz,id,otherQuery} = modelObj
 
  let pageProps = {
    // ---------- 界面部分 ----------
    ui: {
      // 页面接口地址
      api_url: `zituo-admin/a/platform/userManager/userOrgList`,
      // 接口方式
      method: "POST",
      // "data_field_name":"data",
      app: "",
      // 接口参数
      params: {
        pageSize:'10',
        pageNo:'1',
        areaId:otherQuery.areaId && otherQuery.areaId != ""?otherQuery.areaId:"",
        projectId:otherQuery.projectId && otherQuery.projectId != ""?otherQuery.projectId:"",
        // channel: '行销自拓'
        // projectId: '37E4B6B9-2982-4CEC-8B09-CD68C75DA8A6'
        },
      // 模拟数据 - 用于开发需要，若填充了上面的api_url之后，请将此项设置为null
      // "mockData": mockData,
      mockData: null,
      // 查询栏
      search_bar: {
        fields: [
          {
            en_name: 'regionId',
            zh_name: '区域',
            elem_type: 'Select',
            elem_valid_type: 'string',
            cmpt_items: areaList
          },
          {
            en_name: 'projectId',
            zh_name: '项目',
            elem_type: 'SelectFilter',
            elem_valid_type: 'string',
            showSearch: true,
            cmpt_items: products,
            onFocus:()=>{
              if(biz.queryForm && biz.queryForm.regionId){ 
                dispatch({
                  type: `${namespace}/filterProjectList`,
                  payload: {}
                })
              }
            },
          },
          {
            en_name: 'name',
            zh_name: '姓名',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
          {
            en_name: 'loginName',
            zh_name: '账户',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
          {
            en_name: 'phone',
            zh_name: '手机号',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
          {
            en_name: 'parentName',
            zh_name: '归属',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
          {
            en_name: 'appRoleZituo',
            zh_name: '人员类型',
            elem_type: 'Select',
            elem_valid_type: 'string',
            cmpt_items: [
              {
                  value: '1',
                  label: '渠道经理'
               }, 
              {
                  value: '2',
                  label: '渠道主管'
              },
              {
                  value: '3',
                  label: '渠道专员'
              },
              {
                  value: '4',
                  label: '小蜜蜂'
              }
          ],
          },
          {
            en_name: 'channel',
            zh_name: '渠道',
            elem_type: 'Select',
            elem_valid_type: 'string',
            cmpt_items: [
              {
                  value: '行销自拓',
                  label: '行销自拓'
               }
            ],
          },
          {
            en_name: 'status',
            zh_name: '状态',
            elem_type: 'Select',
            elem_valid_type: 'string',
            cmpt_items: [
              {
                  value: '0',
                  label: '禁用'
               }, 
              {
                  value: '1',
                  label: '启用'
              }
            ],
          },
        ],
        searchHandler: () => { // 自定义搜索按钮回调函数，点击搜索时触发
          console.log('search click')
        },
        resetHandler: () => { // 自定义重置按钮回调函数，点击重置时触发
          console.log('reset click')
        },
        searchCallback: params => { // 自定义搜索完成后的后调函数, 搜索完成后触发, 并将本次搜索的参数传入
          console.log('search finish', params)
        },
        pasteTargetList: [  // 列表组件中将输入框的en_name作为默认id(也可以通过id另外设置)
          'channel',  // 将需要粘贴事件拦截的输入框id以数组的形式传入, 该参数对应PasteFromClipboard组件的targetList参数
        ]
      },
      // 数据表格  序号 是否生效 项目名称  佣金方案名称 方案生效日期 方案失效日期 审批人 审批通过日期   提交人 提交日期  创建人 创建日期
      table:
        {
          rowSelectionInfo: {
            type: 'checkbox',
          },
          status_params: "status=0",
          status_text: "全部",
          // 表头字段列表
          scroll: { x: 1200},
          fields: [
            {
              en_name: "ruleNo",
              zh_name: "编号",
              width: 60,
              render: (a, item, index, d, e, f) => {
                return index+1
              },
            },
            {
              en_name: "name",
              zh_name: "姓名",
            },
            {
              en_name: "loginName",
              zh_name: "账户",
            },
            {
              en_name: "phone",
              zh_name: "手机号",
            },
            {
              en_name: "parentName",
              zh_name: "归属",
            },
            {
              en_name: "appRoleZituo",
              zh_name: "人员类型",
              render:(a, item, index, d, e, f)=>{
                if(item.appRoleZituo == 1){
                  return '渠道经理'
                }else if(item.appRoleZituo == 2){
                  return '渠道主管'
                }else if(item.appRoleZituo == 3){
                  return '渠道专员'
                }else if(item.appRoleZituo == 4){
                  return '小蜜蜂'
                }
                // else if(item.appRoleZituo == 5){
                //   return '关键人'
                // }
              }
            },
            {
              en_name: "status",
              zh_name: "状态",
              render:(a, item, index, d, e, f)=>{
                if(item.status == 0){
                  return '禁用'
                }else if(item.status == 1){
                  return '启用'
                }
              }
            },
            {
              en_name: "edit",
              zh_name: "操作",
              width:110,
              fixed:'right',
              render: (a, item, index, d, e, f) => {
                return (
                  <span>
                      {/* <FlagBtn node={<a onClick={() => checkOrEditSkip('edit',item)}>授权<span className="ant-divider" /></a>} btnId={''}> </FlagBtn> */}
                      <FlagBtn node={<a onClick={() => onSwitch(item)}>{item.status == 0 ? '启用' : '禁用'}<span className="ant-divider" /></a>} btnId={''}> </FlagBtn>
                      <FlagBtn node={<a onClick={() => goUserAdd('detail',item)}>编辑<span className="ant-divider" /></a>} btnId={''}> </FlagBtn>
                      <FlagBtn node={<a onClick={() => changePassword(item)}>重置密码</a>} btnId={''}> </FlagBtn>
                  </span>
                )
              },
            }
          ],
        },
      // 操作栏
      action_bar: [
        {
            label: "下载名片码",
            type: "primary",
            onClick: (e, row) => {
                down()
            }
        },
        {
          label: "新增用户",
          type: "primary",
          rowSelection:[],
          onClick: (e) =>{
            hashHistory.push({
              pathname: '/home/expandManage/userAdd',
              query: {}
            });
          }
        }
      ]
    },
  }
   
  //禁用
  function onSwitch(item){
    let val = ''
    item.status==1?val=0:val=1
    dispatch({
      type: `${namespace}/onSwitch`,
      payload: {id:item.id,status:val}
    })
  }
  function down() {
      dispatch({
          type: `${namespace}/down`,
          payload: {}
      })
  }
  //授权
  function checkOrEditSkip(type,item){
      hashHistory.push({
        pathname: '/home/expandManage/userDetail',
        query: {actionType: type, id:item.id }
      })
  }
  //编辑
  function goUserAdd(type,item){ 
      hashHistory.push({
        pathname: '/home/expandManage/userAdd',
        query: {actionType: type, id:item.id }
      })
  }
  //重置密码
  function changePassword(item){
    if(item.appRoleZituo != 4){
      message.warn('只有小蜜蜂才能重置密码')
      return false
    }
    confirm({
      title: '系统提示',
      content: '确定要重置该用户的密码?',
      okText: '确认',
      okType: 'primary',
      cancelText: '取消',
      onOk() {
        dispatch({
          type: `${namespace}/changePassword`,
          payload: {id:item.id}
        })
      },
      onCancel() {
        console.log('Cancel');
      },
    });
   
  }

  if (!!modelObj.biz) {
		pageProps.biz = modelObj.biz
		pageProps.biz.syncBackCbf = (biz) => {
			dispatch({
				type: `${namespace}/updateStore`,
				payload: { biz }
			})
		}
  }

  return (
    <div className={cx('pageLayout')}>
      <ListPage pageProps={pageProps}/>
    </div>
  )
}
// 连接视图(view)和模型(model)
export default connect(mapStateToProps)(Form.create()(userListView))
